<?php
declare(strict_types=1);

namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\FeedbackRepository;
use HiCommon\Repository\CouponsRepository;
use HiCommon\Repository\LogRepository;
use HiCommon\Repository\MessageRepository;
class FeedbackService
{


	/**
	 *	获取反馈列表
	 *	hinq 2019 08 29
	 *
	 **/
	public function get_list( array $arr_search ){
		$arr_where['with'] = ['getUserInfo','admin'];
		$arr_list = FeedbackRepository::get_list( (array) $arr_search , (array) $arr_where );
        $arr_list->appends($arr_search);//添加链接参数
		$arr_list = list_page_data( $arr_list , $arr_search );
		foreach ( $arr_list['data'] as $key=>&$vo ){
		    if( !empty( $vo['imgs'] ) ){
                $vo['imgs'] = explode(',' ,$vo['imgs'] );
            }else{
                $vo['imgs'] = [];
            }
        }
		return hello_success('成功',$arr_list);
	}


	/**
	 *	更新反馈信息状态
	 * 	hinq 2019 08 30
	 *
	 **/
	public function edit_one( array $arrInput ){
		try {
            //编辑
            if( empty( $arrInput['remark'] ) ){
                return hello_error('处理结果不能为空');
            }
            $arrInput['admin_id'] = admin_user_id();
            $arrInput['state'] = 2;
            unset( $arrInput['_token'] );
            $res = FeedbackRepository::edit_one($arrInput);
            if (empty($res)) {
                return hello_error('处理失败');
            }
            return hello_success('处理成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
	}

	/**
	 *  获取平台下所有有效的优惠券
     *  hinq 2019 11 28
     *
     **/
    public function get_coupons_list(){
        $arr_option['where'] = ['status'=>1,'repairstore_id'=>1];
        $arr_option['field'] = ['id','name'];
        $arr_list = CouponsRepository::get_all( (array) $arr_option )->toArray();
        return $arr_list;
    }

    /**
     *  添加、更新注册有礼数据
     *  hinq 2019 11 28
     *
     **/
    public function register_gift_update( array $arr_input ){
        try{
            //php  array_filter()   清除数组中空元素
            $arr_input['coupon_id'] = array_filter( $arr_input['coupon_id'] );
            $arr_input['price'] = array_filter( $arr_input['price'] );
            unset( $arr_input['_token']);

           if( $arr_input['open'] == 'on' ){  //打开
               if( count( $arr_input['coupon_id'] ) == 0 ){
                   return hello_error('信息设置错误');
               }
               foreach ( $arr_input['coupon_id'] as $key=>$vo){
                    if( $vo && empty( $arr_input['price'][$key] ) ){
                        return hello_error('信息设置错误3');
                    }
               }
           }else{  //关闭
               unset( $arr_input );
               $arr_input['open'] = 'off';
               $arr_input['coupon_id'] = [];
               $arr_input['price'] = [];
           }
           $info = LogRepository::get_set_field_info_log( (string) 'register_gift' )->toArray();
           if( !empty( $info['data'][0]['_id'] ) ){   //编辑
               LogRepository::set_field_info_update_log( (string) 'register_gift' , (array) $arr_input );
           }else{
               $arr_input['title'] = 'register_gift';
               LogRepository::set_field_info_log( (array) $arr_input );
           }
           return hello_success('ok');
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  获取设置的注册有礼数据
     *  hinq 2019 11 28
     *
     **/
    public function get_register_gift(){
        $arr_list = LogRepository::get_set_field_info_log( (string) 'register_gift' );
        if( !empty( $arr_list->first() ) ){
            $arr_list = $arr_list->toarray();
            $arr_list['data'][0]['count'] = count( $arr_list['data'][0]['coupon_id'] );
        }else{
            $arr_list = [];
        }
        return $arr_list;
    }

    /**
     *  获取系统消息列表
     *  hinq 2019 12 07
     *
     **/
    public function get_message( array $arr_search ){
        try{
            $arr_option['where'] = ['status'=>1];
            $arr_option['with'] = ['user','shop'];
            $arr_option['order'] = ['key'=>'id','value'=>'desc'];
            $arr_option['field'] = ['id','title','img','content','pid','type','status','is_read','link_id','user_id','shop_id'];
            $arr_list = MessageRepository::get_list( (array) $arr_option ,  (array) $arr_search );
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data( $arr_list , $arr_search );
            return hello_success('成功',$arr_list);
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

}